<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Estudiante extends MN_Controller {

    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->helper(array('url', 'form'));
        $this->load->library('ion_auth');
        $this->load->library('session');
        $this->load->model('Persona_Model');
        $this->load->model('Estudiante_Model');
    }

    public function index() {

        if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
    }

    public function inicio() {
        if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();

        $this->data['estudiantes'] = $this->Estudiante_Model->getListado();
        $this->session->unset_userdata('alumno_sess');
        $this->_render('estudiantes/start');
    }

    public function crear() {
        if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();

        $this->load->library('form_validation');
        $this->_render('estudiantes/new');
    }

    public function actualizar() {
        if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();

        //$this->load->helper(array('url', 'form'));
        $this->load->library('form_validation');

        $opc = $this->uri->segment(3);
        $ide_pers = $this->uri->segment(4);
        if (!isset($_POST['cedula_pers'])) {
            $this->data['estudiante'] = $this->Estudiante_Model->getDatosEstudiante($ide_pers);
            $this->data['opc'] = $opc;
            $this->_render('estudiantes/update');
        } else {
            $ide_pers = $this->input->post('persona_ide_pers');
            $opc = $this->input->post('txtopc');
            $this->data['opc'] = $opc;

            $this->form_validation->set_rules('cedula_pers', 'NUMERO DE CEDULA', 'trim|integer|required|callback_cedula');
            $this->form_validation->set_rules('nombre_pers', 'NOMBRES', 'required|trim');
            $this->form_validation->set_rules('apellido_pers', 'APELLIDOS', 'required|trim');
            $this->form_validation->set_rules('nacionalidad_pers', 'NACIONALIDAD', 'required|trim');
            $this->form_validation->set_rules('provincia_pers', 'PROVINCIA', 'required|trim');
            $this->form_validation->set_rules('canton_pers', 'CANTON', 'required|trim');
            $this->form_validation->set_rules('direccion_pers', 'DIRECCION', 'required|trim');
            $this->form_validation->set_rules('email_pers_est', 'Email personal requerido', 'required|trim|valid_email');
            $this->form_validation->set_rules('planted_procede_est', 'NOMBRE DEL PLANTEL DONDE PROCEDE', 'required|trim');
            $this->form_validation->set_rules('especialidad_est', 'NOMBRE DE LA ESPECIALIDAD', 'required|trim');
            $this->form_validation->set_rules('nombre_madre_est', 'NOMBRE DEL REPRESENTATE', 'required|trim');

            if (($this->form_validation->run() == FALSE)) {
                $this->data['estudiante'] = $this->Estudiante_Model->getDatosEstudiante($ide_pers);

                $this->data['opc'] = $opc;
                //echo "error en ingreso de datos";

                $this->_render('estudiantes/update');
            } else {
                $error_fatal = 0;

                if (!empty($_FILES['foto_pers']['name'])) {
                    $config['upload_path'] = './commons/personas/';
                    $config['allowed_types'] = 'gif|jpg|png|jpeg';
                    $config['max_size'] = '3000';
                    $config['max_width'] = '1024';
                    $config['max_height'] = '768';

                    $this->load->library('upload', $config);

                    if (!$this->upload->do_upload('foto_pers')) {
                        $this->data['msj_error'] = $this->upload->display_errors();
                        $upload_data = null;
                        $error_fatal = 0;
                    } else {
                        $upload_data = $this->upload->data();
                        $error_fatal = 1;
                    }
                } else {
                    $error_fatal = 2;
                }
                switch ($error_fatal) {

                    case 0:
                        $this->_render('estudiantes/update');
                        break;
                    case 1:
                    case 2:
                        $persona = array(
                            "cedula_pers" => $this->input->post('cedula_pers'),
                            "tipo_doc_pers" => $this->input->post('tipo_doc_pers'),
                            "nombre_pers" => $this->input->post('nombre_pers'),
                            "apellido_pers" => $this->input->post('apellido_pers'),
                            "nacionalidad_pers" => $this->input->post('nacionalidad_pers'),
                            "provincia_pers" => $this->input->post('provincia_pers'),
                            //"ciudad_pers" => $this->input->post('ciudad_pers'),
                            "canton_pers" => $this->input->post('canton_pers'),
                            "parroaquia_pers" => $this->input->post('parroquia_pers'),
                            "barrio_pers" => $this->input->post('barrio_pers'),
                            "direccion_pers" => $this->input->post('direccion_pers'),
                            "refer_domic_pers" => $this->input->post('refer_domic_pers'),
                            "tipo_discap_pers" => $this->input->post('tipo_discap_pers'),
                            "conadis_pers" => $this->input->post('conadis_pers'),
                            "porc_disc_pers" => $this->input->post('porc_disc_pers'),
                            "sexo_pers" => $this->input->post('sexo_pers'),
                            "estado_civil_pers" => $this->input->post('estado_civil_pers'),
                            "autodefin_pers" => $this->input->post('autodefin_pers'),
                            "fecha_nac_pers" => date("Y-m-d", strtotime($this->input->post('fecha_nac_pers'))),
                            "fono_pers" => $this->input->post('fono_pers'),
                            "celular_pers" => $this->input->post('celular_pers'),
                            "email_inst_pers" => $this->input->post('email_inst_pers'),
                            "foto_pers" => ($error_fatal == 1 ? base_url() . "commons/personas/" . $_FILES['foto_pers']['name'] : $this->input->post('foto_pers')),
                        );

                        //$persona_ide_pers=1;

                        $estudiante = array(
                            "planted_procede_est" => $this->input->post('planted_procede_est'),
                            "especialidad_est" => $this->input->post('especialidad_est'),
                            "nombre_padre_est" => $this->input->post('nombre_padre_est'),
                            "profesion_padre_est" => $this->input->post('profesion_padre_est'),
                            "nombre_madre_est" => $this->input->post('nombre_madre_est'),
                            "profesion_madre_est" => $this->input->post('profesion_madre_est'),
                            "conjuge_est" => $this->input->post('conjuge_est'),
                            "carga_fam_est" => $this->input->post('carga_fam_est'),
                            "tipo_sangre_est" => $this->input->post('tipo_sangre_est'),
                            "sector_est" => $this->input->post('sector_est'),
                            "email_pers_est" => $this->input->post('email_pers_est'),
                            "face_est" => $this->input->post('face_est'),
                            "twit_est" => $this->input->post('twit_est'),
                            "estud_traba_est" => $this->input->post('estud_traba_est'),
                            "activo_est" => $this->input->post('activo_est'),
                        );


                        if ($opc == 'upd') {
                            $this->Persona_Model->actualizarPersona($persona, $ide_pers);
                            $this->Estudiante_Model->actualizarEstudiante($estudiante, $ide_pers);
                        } else {
                            $this->Estudiante_Model->eliminarEstudiante($ide_pers);
                        }




                        break;
                }
                redirect('estudiante/inicio');
            }
        }
    }

    function presearch() {
        if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();

        $who = $this->input->post('txtsearch');

        if (empty($who))
            redirect("estudiante/inicio");
        else {
            // echo $who." busqueda";

            $this->data['estudiantes'] = $this->Estudiante_Model->getBusquedaEstudiante($who, $this->input->post('tipo_busqueda'));
            $this->_render('estudiantes/start');
        }
    }

    public function ingresodatos() {
        if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();


        $this->load->library('form_validation');

        $this->form_validation->set_rules('cedula_pers', 'NUMERO DE CEDULA', 'trim|integer|required|is_unique[persona.cedula_pers]|callback_cedula');
        $this->form_validation->set_rules('nombre_pers', 'NOMBRES', 'required|trim');
        $this->form_validation->set_rules('apellido_pers', 'APELLIDOS', 'required|trim');
        $this->form_validation->set_rules('nacionalidad_pers', 'NACIONALIDAD', 'required|trim');
        $this->form_validation->set_rules('provincia_pers', 'PROVINCIA', 'required|trim');
        $this->form_validation->set_rules('canton_pers', 'CANTON', 'required|trim');
        $this->form_validation->set_rules('direccion_pers', 'DIRECCION', 'required|trim');
        $this->form_validation->set_rules('email_pers_est', 'Email personal', 'required|trim|valid_email|is_unique[estudiante.email_pers_est]');
        $this->form_validation->set_rules('planted_procede_est', 'NOMBRE DEL PLANTEL DONDE PROCEDE', 'required|trim');
        $this->form_validation->set_rules('especialidad_est', 'NOMBRE DE LA ESPECIALIDAD', 'required|trim');
        $this->form_validation->set_rules('nombre_madre_est', 'NOMBRE DEL REPRESENTATE', 'required|trim');


        if (($this->form_validation->run() == FALSE)) {
            //echo "algun error";
           
           $this->data['tipo_doc_pers']= $this->input->post('tipo_doc_pers');
           $this->data['provincia_pers']= $this->input->post('provincia_pers');
           $this->data['parroquia_pers']= $this->input->post('parroquia_pers');
           $this->data['barrio_pers']= $this->input->post('barrio_pers');
           $this->data['refer_domic_pers']= $this->input->post('refer_domic_pers');
           $this->data['tipo_discap_pers']= $this->input->post('tipo_discap_pers');
           $this->data['conadis_pers']= $this->input->post('conadis_pers');
           $this->data['porc_disc_pers']= $this->input->post('porc_disc_pers');
           
           
           $this->data['sexo_pers']= $this->input->post('sexo_pers');
           $this->data['estado_civil_pers']= $this->input->post('estado_civil_pers');
           $this->data['autodefin_pers']= $this->input->post('autodefin_pers');
           $this->data['fecha_nac_pers']= $this->input->post('fecha_nac_pers');
           $this->data['fono_pers']= $this->input->post('fono_pers');
           $this->data['celular_pers']= $this->input->post('celular_pers');
           $this->data['email_inst_pers']= $this->input->post('email_inst_pers');
           $this->data['nombre_padre_est']= $this->input->post('nombre_padre_est');
           $this->data['profesion_padre_est']= $this->input->post('profesion_padre_est');
           $this->data['profesion_madre_est']= $this->input->post('profesion_madre_est');
           $this->data['conjuge_est']= $this->input->post('conjuge_est');
           $this->data['carga_fam_est']= $this->input->post('carga_fam_est');
           $this->data['tipo_sangre_est']= $this->input->post('tipo_sangre_est');
           $this->data['sector_est']= $this->input->post('sector_est');
           $this->data['face_est']= $this->input->post('face_est');
           $this->data['twit_est']= $this->input->post('twit_est');
           $this->data['estud_traba_est']= $this->input->post('estud_traba_est');
           
           
           
           
           
           

            $this->_render('estudiantes/new');
        } else {
            $username = substr($this->input->post('nombre_pers'), 0, strpos($this->input->post('nombre_pers'), " "))
                    . substr($this->input->post('apellido_pers'), 0, 1)
                    . substr(md5(microtime()), 1, 3);

            //$v=!empty($this->input->post('foto_pers'));
            //if (isset($_FILES['image']) && !empty($_FILES['image']['name']))

            $error_fatal = 0;

            if (!empty($_FILES['foto_pers']['name'])) {
                $config['upload_path'] = './commons/personas/';
                $config['allowed_types'] = 'gif|jpg|png|jpeg';
                $config['max_size'] = '3000';
                $config['max_width'] = '1024';
                $config['max_height'] = '768';

                $this->load->library('upload', $config);

                if (!$this->upload->do_upload('foto_pers')) {
                    $this->data['msj_error'] = $this->upload->display_errors();
                    $upload_data = null;
                    $error_fatal = 0;
                } else {
                    $upload_data = $this->upload->data();
                    $error_fatal = 1;
                }
            } else {
                $error_fatal = 2;
            }
            switch ($error_fatal) {

                case 0:
                    $this->_render('estudiantes/new');
                    break;
                case 1:
                case 2:
                    $persona = array(
                        "cedula_pers" => $this->input->post('cedula_pers'),
                        "tipo_doc_pers" => $this->input->post('tipo_doc_pers'),
                        "nombre_pers" => $this->input->post('nombre_pers'),
                        "apellido_pers" => $this->input->post('apellido_pers'),
                        "nacionalidad_pers" => $this->input->post('nacionalidad_pers'),
                        "provincia_pers" => $this->input->post('provincia_pers'),
                        //"ciudad_pers" => $this->input->post('ciudad_pers'),
                        "canton_pers" => $this->input->post('canton_pers'),
                        "parroaquia_pers" => $this->input->post('parroquia_pers'),
                        "barrio_pers" => $this->input->post('barrio_pers'),
                        "direccion_pers" => $this->input->post('direccion_pers'),
                        "refer_domic_pers" => $this->input->post('refer_domic_pers'),
                        "tipo_discap_pers" => $this->input->post('tipo_discap_pers'),
                        "conadis_pers" => $this->input->post('conadis_pers'),
                        "porc_disc_pers" => $this->input->post('porc_disc_pers'),
                        "sexo_pers" => $this->input->post('sexo_pers'),
                        "estado_civil_pers" => $this->input->post('estado_civil_pers'),
                        "autodefin_pers" => $this->input->post('autodefin_pers'),
                        "fecha_nac_pers" => date("Y-m-d", strtotime($this->input->post('fecha_nac_pers'))),
                        "fono_pers" => $this->input->post('fono_pers'),
                        "celular_pers" => $this->input->post('celular_pers'),
                        "email_inst_pers" => $this->input->post('email_inst_pers'),
                        "foto_pers" => ($error_fatal == 1 ? base_url() . "commons/personas/" . $_FILES['foto_pers']['name'] : base_url() . "commons/personas/foto.jpg"),
                        "username_pers" => $username);

                    //$persona_ide_pers=1;
                    $persona_ide_pers = $this->Persona_Model->grabarPersona($persona);
                    $estudiante = array(
                        "persona_ide_pers" => $persona_ide_pers,
                        "planted_procede_est" => $this->input->post('planted_procede_est'),
                        "especialidad_est" => $this->input->post('especialidad_est'),
                        "nombre_padre_est" => $this->input->post('nombre_padre_est'),
                        "profesion_padre_est" => $this->input->post('profesion_padre_est'),
                        "nombre_madre_est" => $this->input->post('nombre_madre_est'),
                        "profesion_madre_est" => $this->input->post('profesion_madre_est'),
                        "conjuge_est" => $this->input->post('conjuge_est'),
                        "carga_fam_est" => $this->input->post('carga_fam_est'),
                        "tipo_sangre_est" => $this->input->post('tipo_sangre_est'),
                        "sector_est" => $this->input->post('sector_est'),
                        "email_pers_est" => $this->input->post('email_pers_est'),
                        "face_est" => $this->input->post('face_est'),
                        "twit_est" => $this->input->post('twit_est'),
                        "estud_traba_est" => $this->input->post('estud_traba_est'),
                        "activo_est" => $this->input->post('activo_est'),
                    );



                    $this->Estudiante_Model->grabarEstudiante($estudiante);
                    
                    if ($this->session->userdata('alumno_sess')) {
                        // $this->session->set_userdata('alumno_sess', $cod_est);
                        $alumno_sess = $this->session->userdata('alumno_sess');
                        $frm_name=$alumno_sess['name_form'];
                        $alumno_sess['persona_ide_pers'] = $persona_ide_pers;
                        $this->session->unset_userdata('alumno_sess');
                        
                        $this->session->set_userdata('alumno_sess', $alumno_sess);

                        //print_r($alumno_sess);
                        redirect($frm_name);
                        //$periodo = $ses_matr1['ide_per'];
                    }else{
                        redirect('estudiante/inicio');    
                    }


                    


                    break;
            }
        }
    }

    function cedula() {
        if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();



        if ($this->input->post('tipo_doc_pers') == 0) {
            $this->load->helper('cedula');
            if (verificar_cedula($this->input->post('cedula_pers'))) {
                return TRUE;
            } else {
                $this->form_validation->set_message('cedula', '%s INCORRECTO');
                return FALSE;
            }
        } else {
            return TRUE;
        }
    }

}

/* End of file welcome.php */
    /* Location: ./application/controllers/welcome.php */



    